package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.AftersaleDao;
import com.wn.class30.mapper.UserDao;
import com.wn.class30.service.AfterSaleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AfterSaleServiceImpl implements AfterSaleService {
    @Resource
    private AftersaleDao aftersaleDao;
    @Resource
    private UserDao userDao;
    @Override
    public List<Aftersale> selectAfterSales(Map<String,String> map) throws Exception {
        return aftersaleDao.selectByExample(getAfterSaleExample(map));
    }

    @Override
    public Long getAfterSaleCount(Map<String, String> map) throws Exception {
        return aftersaleDao.countByExample(getAfterSaleExample(map));
    }

    @Override
    public void updateAfterById(Aftersale aftersale) throws Exception {
        aftersaleDao.updateByPrimaryKey(aftersale);
    }

    @Override
    public Aftersale selectAfterSaleById(Integer id) throws Exception {
        return aftersaleDao.selectByPrimaryKey(id);
    }

    public AftersaleExample getAfterSaleExample(Map<String,String> map)throws Exception{
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String myorderNumber = map.get("myorderNumber");
        String username = map.get("username");
        String auditingstatus =map.get("auditingstatus");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        AftersaleExample example = new AftersaleExample();
        example.setLimit(limit);
        example.setOffset(page);
        AftersaleExample.Criteria criteria = example.createCriteria();
        if(!"".equals(myorderNumber) && myorderNumber != null){
            criteria.andOrderNumberLike("%"+myorderNumber+"%");
        }
        if(!"".equals(username) && username != null){
            UserExample userExample = new UserExample();
            UserExample.Criteria userExampleCriteria = userExample.createCriteria();
            userExampleCriteria.andUsernameLike("%"+username+"%");
            List<User> users = userDao.selectByExample(userExample);
            if(users.size() > 0){
                for (User user : users) {
                    criteria.andUserIdEqualTo(user.getUserId());
                }
            }else{
                criteria.andUserIdEqualTo(-1);
            }
        }

        if(!"".equals(auditingstatus) && auditingstatus != null && Integer.parseInt(auditingstatus) != 0){
            criteria.andAuditingstatusEqualTo(Integer.parseInt(auditingstatus));
        }

        if(!"".equals(startDate) && startDate != null){
            Date date = simpleDateFormat.parse(startDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andCreatetimeGreaterThanOrEqualTo(simpleDateFormat.parse(format));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 2);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andCreatetimeLessThanOrEqualTo(simpleDateFormat.parse(format));
        }

        return example;
    }
}
